// pages/groupbuy/groupbuy.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select: 0,
    shoplist:'',
    num:0,
    nums:0,
    bnyr:'',
    enyr:'',
    zitiname:'',
    zitiids:'',
    index:'',
    zitinamethree:'',
    zitiidsthree:''
  },
  // 生成订单
  shengcheng:function(){
    // if()
    // for(var a )
    // GroupbuyingTicketID团购券ID
    // GroupbuyingID发布团购ID
    // UType发布类型: 0智客通 1经销商 2商户 3代理商
    // iCode订单编号 默认传空字符
    // ShopID自提商户ID
    // ORCodeIMG订单提货二维码 默认传空字符
    // IsFirst消费者本月第一个订单 0: 不是 1: 是 默认传0
    // OrderNumber数量
    var array = this.data.shoplist;
    var gtid,gid,type,ico,sid,orimg,isf,odnum;
    for(var a in array){
      console.log(array[a].ischeck)
      if (array[a].ischeck == true){
        gtid = array[a].ID,
        gid = array[a].DealerID,
        type = array[a].MType,
        ico='',
        sid = this.data.zitiids, 
        orimg='', 
        isf=0, 
        odnum = array[a].number;
      }
    }
    var obj={
      GroupbuyingTicketID: gtid,
      GroupbuyingID: gid,
      UType: type,
      iCode: ico,
      ShopID: sid,
      ORCodeIMG: orimg,
      IsFirst: isf,
      OrderNumber: odnum
    }
    var queue=[];
    queue.push(obj)
    ajax('/api/OrderCareful/TGOrderInsert?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      queue:queue
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "post",
      }, res => {
        // console.log(res)
        wx.showToast({
          title: res.Message,
          icon: 'succes',
          duration: 2000,
          // mask: true
        })
      })
  },
  // 点击取消按钮
  clickdanxuans:function(e){
    var index = e.currentTarget.dataset.index
    var array = this.data.shoplist;
    for (var a in array) {
      if (a == index) {
        var ischeck = "shoplist[" + a + "].ischeck"//这里需要将设置的属性用字符串进行拼接
        console.log(this.data.shoplist[a].ischeck, 123)
        this.setData({
          [ischeck]: !this.data.shoplist[a].ischeck
        })
      }
    }
  },
  // 点击选中按钮
  clickdanxuan:function(e){
    var index = e.currentTarget.dataset.index
    var array = this.data.shoplist;
    for (var a in array){
      var ischeck = "shoplist[" + a + "].ischeck";
      this.setData({
        [ischeck]: false
      })
      if (a == index) {
        var ischeck = "shoplist[" + a + "].ischeck"//这里需要将设置的属性用字符串进行拼接
        console.log(this.data.shoplist[a].ischeck,123)
        this.setData({
          [ischeck]: !this.data.shoplist[a].ischeck
        })
      }
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })


    this.getone()
  
    
    // if (wx.getStorageSync('zitiname')){
      // console.log(wx.getStorageSync('zitiname'))
      if(options.select){
        this.setData({
          select:options.select
        })
        if(this.data.select==1){
          console.log(1231)
          this.setData({
            zitiname: options.zitiname,
            zitiids: options.zitiids,
            index: options.index
          })
          this.selecttabtwo()
          
          
        }
        if(this.data.select == 2){
          this.setData({
            zitinamethree: options.zitiname,
            zitiidsthree: options.zitiids,
            index: options.index
          })
          this.selecttabthree()
        }
      }
      
    
  },
  selecttabone:function(e){
    let idx = e.currentTarget.dataset.index;
    this.setData({
      select: idx
    })
    if(this.data.select == 0){
      this.getone()
    }else if(this.data.select == 1){
      this.selecttabtwo()
    }else{
      this.selecttabthree()
    }
    
  },
  getone:function(){
    ajax('/api/GroupbuyingTicket/Get?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      CheckFlag: 0,
      PageIndex: 0,
      ProvinceName: wx.getStorageSync('address_component').province,
      CityName: wx.getStorageSync('address_component').city,
      DistrictName: wx.getStorageSync('address_component').district
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        // console.log(res)
        this.setData({
          shoplist: res.Models
        })
      })
  },
  selecttabtwo: function (e) {
    // console.log(e,123)
    // let idx = e.currentTarget.dataset.index;
    // this.setData({
    //   select:idx
    // })
    
    ajax('/api/GroupbuyingTicket/Get?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      CheckFlag: 1,
      PageIndex: 0,
      ProvinceName: wx.getStorageSync('address_component').province,
      CityName: wx.getStorageSync('address_component').city,
      DistrictName: wx.getStorageSync('address_component').district
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        console.log(res)
        // this.setData({
        //   shoplist: res.Models
        // })
        
        if (res.Model!=null){
         for (var a in res.Models) {
           res.Models[a].ischeck = false
           res.Models[a].number = 0
           // if(this.data.zitiname !== ''){
           //   res.Models[a].zitiname = this.data.zitiname
           //   res.Models[a].zitiid = this.data.zitiname
           // }
           res.Models[a].zitiname = ''
           res.Models[a].zitiid = ''
           this.setData({
             bnyr: this.data.shoplist[a].TakedeliverygoodsBeginDate.slice(0, 10),
             enyr: this.data.shoplist[a].TakedeliverygoodsEndDate.slice(0, 10),
             shoplist: res.Models
           })

         }
       }else{
         this.setData({
           shoplist: res.Models
         })
       }
        if (this.data.zitiname !== ''){
          var index = this.data.index
          var array = this.data.shoplist;
          console.log(array, 'array')
          console.log(index,'index')
          for (var a in array) {
            console.log(array, 123456789)
            var zitinames = "shoplist[" + index + "].zitiname";
            var zitiids = "shoplist[" + index + "].zitiid";
            // console.log(zitiids, 123)
            this.setData({
              [zitinames]: this.data.zitiname,
              [zitiids]: this.data.zitiids
            })
          }
        }

      })
      
  },
  selecttabthree: function (e) {
    // let idx = e.currentTarget.dataset.index;
    // this.setData({
    //   select: idx
    // })
    ajax('/api/GroupbuyingTicket/Get?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      CheckFlag: 2,
      PageIndex: 0,
      ProvinceName: wx.getStorageSync('address_component').province,
      CityName: wx.getStorageSync('address_component').city,
      DistrictName: wx.getStorageSync('address_component').district
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        // console.log(res)
        if (res.Models!=null){
         for (var a in res.Models) {
           res.Models[a].ischeck = false
           res.Models[a].number = 0
           // if(this.data.zitiname !== ''){
           //   res.Models[a].zitiname = this.data.zitiname
           //   res.Models[a].zitiid = this.data.zitiname
           // }
           res.Models[a].zitiname = ''
           res.Models[a].zitiid = ''
           this.setData({
             bnyr: this.data.shoplist[a].TakedeliverygoodsBeginDate.slice(0, 10),
             enyr: this.data.shoplist[a].TakedeliverygoodsEndDate.slice(0, 10),
             shoplist: res.Models
           })

         }
       }else{
          this.setData({
            shoplist: res.Models
          })
       }
        if (this.data.zitiname !== '' ) {
          var index = this.data.index
          var array = this.data.shoplist;
          console.log(array, 'array')
          console.log(index, 'index')
          for (var a in array) {
            console.log(array, 123456789)
            var zitinames = "shoplist[" + index + "].zitiname";
            var zitiids = "shoplist[" + index + "].zitiid";
            // console.log(zitiids, 123)
            this.setData({
              [zitinames]: this.data.zitinamethree,
              [zitiids]: this.data.zitiidsthree
            })
          }
        }
      })
  },
  // 跳转自提点
  zitidian:function(e){
    var zitiid = e.currentTarget.dataset.zitiid
    var select = this.data.select
    console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index
    // console.log(select,'123')
    wx.redirectTo({
      url: '/pages/selectziaddress/selectziaddress?id=' + zitiid + '&select=' + select + '&index=' + index,
    })
  },
  // 加
  addnum:function(e){
    var index = e.currentTarget.dataset.id
    var maxnum = e.currentTarget.dataset.pid
    var array = this.data.shoplist;
    for (var a in array) {
      var num = array[a].number
      if (a == index) {
        var number = "shoplist[" + a + "].number"//这里需要将设置的属性用字符串进行拼接
        if (num == maxnum){
          // console.log(this.data.shoplist[a].ischeck, 123)
          this.setData({
            [number]: maxnum
          })
        }else{
          this.setData({
            [number]: num + 1
          })
        }
        
      }
    }
  },
  // 减
  reducenum:function(e){
    var index = e.currentTarget.dataset.id
    var minnum = 0
    var array = this.data.shoplist;
    for (var a in array) {
      var num = array[a].number
      if (a == index) {
        var number = "shoplist[" + a + "].number"//这里需要将设置的属性用字符串进行拼接
        if (num == minnum) {
          // console.log(this.data.shoplist[a].ischeck, 123)
          this.setData({
            [number]: minnum
          })
        } else {
          this.setData({
            [number]: num - 1
          })
        }

      }
    }
  },
  // 清空
  cleannum:function(e){
    var index = e.currentTarget.dataset.index
    var array = this.data.shoplist;
    for (var a in array) {
      if (a == index) {
        var number = "shoplist[" + a + "].number";
        this.setData({
          [number]: 0
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})